import { Command } from 'commander'
import chalk from 'chalk'
import { execSync } from 'child_process'

export const buildCommand = new Command('build')
  .description('Build the project for production')
  .option('-w, --watch', 'watch for changes')
  .option('-m, --minify', 'minify output', true)
  .action(async options => {
    try {
      console.log(chalk.blue('🔨 Building project...'))

      const watchFlag = options.watch ? '--watch' : ''
      const minifyFlag = options.minify ? '--minify' : ''

      // Run build command
      const buildCmd = `vite build ${watchFlag} ${minifyFlag}`.trim()

      console.log(chalk.gray(`Running: ${buildCmd}`))

      execSync(buildCmd, {
        stdio: 'inherit',
        cwd: process.cwd(),
      })

      if (!options.watch) {
        console.log(chalk.green('✅ Build completed successfully!'))
      }
    } catch (error) {
      console.error(chalk.red('❌ Build failed:'), error)
      process.exit(1)
    }
  })
